package com.szl.yeb.controller;

import com.szl.yeb.common.constant.MsgConstant;
import com.szl.yeb.common.R;
import com.szl.yeb.entity.Role;
import com.szl.yeb.service.MenuRoleService;
import com.szl.yeb.service.MenuService;
import com.szl.yeb.service.RoleService;
import com.szl.yeb.utils.ResponseProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: yeb
 * @description: 权限组管理接口
 * @author: szl
 * @create: 2022-08-27 09:15
 **/
@RestController
@Api(tags = "权限管理接口(PermissionController)")
@RequestMapping("system/basic/per")
public class PermissionController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuRoleService menuRoleService;

    @ApiOperation("查询所有角色")
    @GetMapping("/role")
    public R<Object> getRoles() {
        return R.obj(roleService.list());
    }

    @ApiOperation(value = "新增角色信息 ")
    @PostMapping("/role")
    public R<Object> addRole(@RequestBody Role role) {
        return ResponseProcessor.getResWithMsg(roleService.save(role), MsgConstant.ADD);
    }

    @ApiOperation(value = "删除角色信息")
    @DeleteMapping("/role/{id}")
    public R<Object> deleteRole(@PathVariable("id") Integer rid) {
        return ResponseProcessor.getResWithMsg(roleService.deleteRoleAndAssociations(rid), MsgConstant.DELETE);
    }

    @ApiOperation("查询所有菜单及其子菜单")
    @GetMapping("/menu")
    public R<Object> getMenusWithChildren() {
        return R.obj(menuService.getMenusWithChildren());
    }

    @ApiOperation("查询角色的菜单id")
    @GetMapping("/menu/{rid}")
    public R<Object> getMenusFromRoleId(@PathVariable("rid") Integer rid) {
        return R.obj(menuService.getMenusFromRoleId(rid));
    }

    @ApiOperation("更新角色菜单")
    @PutMapping("/menu")
    public R<Object> updateMenuRoleAssociations(Integer rid,@RequestBody List<Integer> mids) {
        return ResponseProcessor.getResWithMsg(menuRoleService.updateMenuRoleAssociations(rid,mids), MsgConstant.UPDATE);
    }
}

